<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
defined('_JEXEC') or die('');

class ArticleflowHelper {

    var $_user = null;

    function buildUser($select, $assigned_user='assigned_user', $javascript='') {
       // echo 'lua chon: ' . $select;
        $db = & JFactory::getDBO();
        $role = ArticleflowHelper::getUserRole();
        $userOptions = array();
        if ($role == 0 || $role == 2) {
            $query = "SELECT * FROM #__users WHERE gid = 23 AND params LIKE '%role=1%'";
            $db->setQuery($query);
            $rows = $db->loadObjectList();

            $userOptions[] = JHTML::_('select.optgroup', "Nguoi xet duyet");
            foreach ($rows as $item) {
                $userOptions[] = JHTML::_('select.option', $item->id, $item->name);
            }
        } else if ($role == 1) {
            $query = "SELECT * FROM #__users WHERE gid = 23 AND params LIKE '%role=0%' ";
            $query1 = "SELECT * FROM #__users WHERE gid = 23 AND params LIKE '%role=2%' ";

            $db->setQuery($query);
            $rows = $db->loadObjectList();
            $db->setQuery($query1);
            $rows2 = $db->loadObjectList();

            $userOptions[] = JHTML::_('select.optgroup', "Nguoi bien tap");
            foreach ($rows as $item) {
                $userOptions[] = JHTML::_('select.option', $item->id, $item->name);
            }
            // $userOptions[] = JHTML::_('select.option','');
            $userOptions[] = JHTML::_('select.optgroup', JText::_("Nguoi cong bo"));
            foreach ($rows2 as $item2) {
                $userOptions[] = JHTML::_('select.option', $item2->id, $item2->name);
            }
        }

        if ($javascript) {
            $javascript = 'onchange="document.adminForm.submit();"';
            $userOptions[] = JHTML::_('select.option', '', '---Select User ---');
        }


        return JHTML::_('select.genericlist', $userOptions, $assigned_user, 'class="inputbox" ' . $javascript, 'value', 'text', $select);
    }
    
     function buildUserName($select, $assigned_user='assigned_user', $javascript='') {
     
        $db = & JFactory::getDBO();
        $role = ArticleflowHelper::getUserRole();
        $userOptions = array();
        if ($role == 0 || $role == 2) {
            $query = "SELECT * FROM #__users WHERE gid = 23 AND params LIKE '%role=1%'";
            $db->setQuery($query);
            $rows = $db->loadObjectList();

            $userOptions[] = JHTML::_('select.optgroup', "Nguoi xet duyet");
            foreach ($rows as $item) {
                $userOptions[] = JHTML::_('select.option', $item->id, $item->name);
            }
        } else if ($role == 1) {
            $query = "SELECT * FROM #__users WHERE gid = 23 AND params LIKE '%role=0%' ";
            $query1 = "SELECT * FROM #__users WHERE gid = 23 AND params LIKE '%role=2%' ";

            $db->setQuery($query);
            $rows = $db->loadObjectList();
            $db->setQuery($query1);
            $rows2 = $db->loadObjectList();

            $userOptions[] = JHTML::_('select.optgroup', "Nguoi bien tap");
            foreach ($rows as $item) {
                $userOptions[] = JHTML::_('select.option', $item->id, $item->name);
            }
            // $userOptions[] = JHTML::_('select.option','');
            $userOptions[] = JHTML::_('select.optgroup', JText::_("Nguoi cong bo"));
            foreach ($rows2 as $item2) {
                $userOptions[] = JHTML::_('select.option', $item2->id, $item2->name);
            }
        }

        if ($javascript) {
            $javascript = 'onchange="document.adminForm.submit();"';
            $userOptions[] = JHTML::_('select.option', '', '---Select User ---');
        }


        return JHTML::_('select.genericlist', $userOptions, $assigned_user, 'class="inputbox" ' . $javascript, 'value', 'text', $select);
    }

    function buildGroupUserByRole() {
        
    }

    function buildCategory($select, $catid='catid', $javascript = '') {
        $db = & JFactory::getDBO();
        $query = 'SELECT id, title'
                . ' FROM #__categories'
                . ' WHERE section = ' . $db->Quote($select)
                . ' AND published = 1'
        ;
        //$query = 'SELECT * FROM #__tip_categories WHERE published = 1 ';
        $db->setQuery($query);
        $rows = $db->loadObjectList();

        $catOptions = array();
        if ($javascript) {
            $javascript = 'onchange="document.adminForm.submit();"';
            $catOptions[] = JHTML::_('select.option', '0', '- ' . JText::_('Select a Category') . ' -');
        }
        foreach ($rows as $item) {
            $catOptions[] = JHTML::_('select.option', $item->id, $item->title);
        }

        return JHTML::_('select.genericlist', $catOptions, $catid, 'class="inputbox" ' . $javascript, 'value', 'text', $select);
    }

    function buildSection() {
        $contentSection = '';
        $db = & JFactory::getDBO();
        $query = 'SELECT s.id, s.title' .
                ' FROM #__sections AS s'

        ;
        $db->setQuery($query);
        $results[] = JHTML::_('select.option', '-1', '- ' . JText::_('Select Section') . ' -', 'id', 'title');
        $results[] = JHTML::_('select.option', '0', JText::_('Uncategorized'), 'id', 'title');
        $results = array_merge($results, $db->loadObjectList());
        return $results;
    }

    function buildListCategory($select, $name='catid', $javascript='') {
        $db = & JFactory::getDBO();

        $query = 'SELECT * FROm #__categories WHERE published =1';
        $db->setQuery($query);
        $rows = $db->loadObjectList();

        $catOptions = array();
        if ($javascript) {
            $javascript = 'onchange="document.adminForm.submit();"';
            $catOptions[] = JHTML::_('select.option', '', '---Lựa chọn danh mục---');
        }
        foreach ($rows as $item) {
            $catOptions[] = JHTML::_('select.option', $item->id, $item->title);
        }

        return JHTML::_('select.genericlist', $catOptions, $name, 'class="inputbox" ' . $javascript, 'value', 'text', $select);
    }

    function buildStateList($filter_state='*', $published='Published', $unpublished='Unpublished', $complete='Complete', $archived=NULL, $trashed=NULL) {
        $state[] = JHTML::_('select.option', '', '- ' . JText::_('Trạng thái bài viết') . ' -');
        //Jinx : Why is this used ?
        //$state[] = JHTML::_('select.option',  '*', JText::_( 'Any' ) );
        $state[] = JHTML::_('select.option', 'P', JText::_("Đang xét duyệt"));
        $state[] = JHTML::_('select.option', 'U', JText::_("Đang biên tập"));
        $state[] = JHTML::_('select.option', 'C', JText::_("Hoàn tất"));
        if ($archived) {
            $state[] = JHTML::_('select.option', 'A', JText::_($archived));
        }

        if ($trashed) {
            $state[] = JHTML::_('select.option', 'T', JText::_($trashed));
        }
        // print_r($state);
        return JHTML::_('select.genericlist', $state, 'filter_state', 'class="inputbox" size="1" onchange="submitform( );"', 'value', 'text', $filter_state);
    }

    function CheckStateByUserID($userID, $role0, $role2) {
        $db = & JFactory::getDBO();

        $query = "SELECT * FROM #__users WHERE id=" . $userID . " AND params LIKE '" . $role2 . "'";
        //$query = 'SELECT * FROM #__tip_categories WHERE published = 1 ';
        $db->setQuery($query);
        $this->_user = $db->loadObject();

        echo $this->_user->params;
        die();
    }

    function saveComplete($task = 'saveComplete', $alt = 'Hoàn tất') {
        $bar = & JToolBar::getInstance('toolbar');
        // Add a save button
        $bar->appendButton('Standard', 'save', $alt, $task, false, false);
    }

    function UserReviews($task = 'SendReview', $alt = 'Send Review') {
        $bar = & JToolBar::getInstance('toolbar');
        // Add a save button
        $bar->appendButton('Standard', 'save', $alt, $task, false, false);
    }

    function UserPublished($task = 'SendPublished', $alt ='Gửi Biên tập hoặc Công bố') {
        $bar = & JToolBar::getInstance('toolbar');
        // Add a save button
        $role = ArticleflowHelper::getUserRole();
        if ($role == 1) {
            $alt = 'Gửi Biên tập hoặc Công bố';
        } else if ($role == 0 || $role == 2) {
            $alt = 'Gửi Xét Duyệt   ';
        }
        $bar->appendButton('Standard', 'save', $alt, $task, false, false);
    }

    function ViewHistory($task = 'ViewHistory', $alt = 'Lịch sử bài viết') {
        $bar = & JToolBar::getInstance('toolbar');
        // Add a save button
        $bar->appendButton('Standard', 'save', $alt, $task, false, false);
    }

    function Previous($task = 'Previous', $alt = 'Quay lại') {
        $bar = & JToolBar::getInstance('toolbar');
        // Add a save button
        $bar->appendButton('Standard', 'save', $alt, $task, false, false);
    }

    function getUserRole($id=null) {
        $user = & JFactory::getUser($id);
        $role = $user->getParam('role', 0);
        return $role;
    }

}

?>
